import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np

# 过去7天的天气和温度数据
# 天气用数字表示：晴天->0, 多云->1, 雨天->2, 阴天->3
weather = np.array([0, 1, 2, 3, 0, 1, 0], dtype=np.float32)
temperature = np.array([28, 26, 22, 24, 30, 27, 29], dtype=np.float32)

# 转换为 PyTorch 的 Tensor
weather_tensor = torch.from_numpy(weather)
temperature_tensor = torch.from_numpy(temperature)

# 构建神经网络模型
class WeatherPredictor(nn.Module):
    def __init__(self):
        super(WeatherPredictor, self).__init__()
        self.fc = nn.Linear(1, 1)  # 单个输入特征，单个输出特征

    def forward(self, x):
        x = self.fc(x)
        return x

model = WeatherPredictor()

# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 训练模型
for epoch in range(1000):
    optimizer.zero_grad()
    output = model(weather_tensor)
    loss = criterion(output, temperature_tensor)
    loss.backward()
    optimizer.step()

# 使用模型进行预测
predicted_weather = torch.Tensor([0])  # 预测明天的天气
predicted_temperature = model(predicted_weather)  # 预测明天的温度

print(predicted_weather, predicted_temperature.item())


if __name__=='__main__':
    print("==over==")
